<%@ page isELIgnored ="false" %>
<%@ page language="java" pageEncoding="UTF-8"%>
<html> 
<head>
<meta http-equiv="Content-Type" content="text/jspl" charset="utf-8">
<%
String path = request.getContextPath();
pageContext.setAttribute("path",path);

pageContext.setAttribute("prefix","_editRole_");
%>
<script> 

function updateRole()
{
	var data ={};
	data["name"]=document.getElementById("${prefix}roleName").value;
	data["memo"]=document.getElementById("${prefix}memo").value;
	data["nonLocked"]=document.getElementById("${prefix}nonLocked").checked?1:0;
	data["deleteFlag"]=false;
	var fakeAuths=[];
	
	var nodes = document.getElementsByName("${prefix}auth");

	var authsData="";
	for (var i=0;i<nodes.length ;++i )
	{
		if (nodes[i].checked==true){
			fakeAuths.push({"id":nodes[i].value})
		}
	}
	data["fakeAuths"]=fakeAuths;

	HMSRest.AjaxPut("${path}/service/role/${param['roleId']}",dojo.toJson(data),function(response,ioArgs){
		if (response.errorCode==0){
			alert("修改角色成功！");
			
		}else{
			if (response.errorCode==1){
				alert("角色名称冲突,请选择其它的角色名称！");
				document.getElementById("roleName").value="";
			}
		}
		});
	 return true; 
}

function getAllAuthInEditRole()
{
	HMSRest.AjaxGet("${path}/service/auths",function(response,ioArgs)
	{
		var cols=2;
		var innerHtml ="<table id=\"tblAuths\"  align=\"center\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\">";
		var data = response.items;
		var i=0;
		for (;i<response.count;++i){
			if (i%cols == 0)
			{
				innerHtml += "<tr>";
			}
			innerHtml += "<td>" + data[i].name +"&nbsp<input type='checkbox' name='${prefix}auth' value=\""+data[i].id+"\" />&nbsp;&nbsp; </td>";
			if (i%cols == cols-1)
			{
				innerHtml += "</tr>";
			}
		}
		var mod = i%cols;
		if (mod >0 )
		{
			for (i=0;i<mod-1 ;++i )
			{
				innerHtml += "<td>&nbsp;</td>";
			}
		}
		innerHtml += "</tr>";
		innerHtml +="</table>";
		document.getElementById("${prefix}auths").innerHTML=innerHtml;
	},function(response,ioArgs){alert(response)});
	
	HMSRest.AjaxGet("${path}/service/role/fetch/"+${param['roleId']},function(response,ioArgs)
		{
			 document.getElementById("${prefix}roleName").value=response.name;
			 document.getElementById("${prefix}memo").value=response.memo;
			 if (response.nonLocked=1)
			 {
				 document.getElementById("${prefix}nonLocked").checked=true;
			 }else{
				  document.getElementById("${prefix}nonLocked").checked=false;
			 }
			 var nodes = document.getElementsByName("${prefix}auth");
			 var authsData = response.fakeAuths;
			 for (var i=0;i<nodes.length ;++i )
			 {
				 var authId = nodes[i].value;
				for (var j=0;j<authsData.length;++j )
				{
					if (authId == authsData[j].id)
					{
						nodes[i].checked=true;
					}
				}
			 }
		}
		,function(response,ioArgs){alert(response)});
}

</script> 
</head>
<body> 
<form id="roleInfo"  action="/hms/service/role" method="POST"> 
<TABLE width="400px"  border="0" cellspacing="0" cellpadding="0" align="center">
<tr><td align="left">基本信息:</td></tr>
<tr><td>
<TABLE  border="1" cellspacing="0" cellpadding="0" align="center">
<TR>
	<TD align="right">角色名称:&nbsp;</TD><TD>&nbsp;<input type="textfield" name="name" id="${prefix}roleName"/>
		<!-- <input type="button" value="" onclick="isExistSuchRoleName()"/>-->
		</TD></TR>
	<TD align="right">是否开锁:&nbsp;</TD><TD>&nbsp;<input type="checkbox" name="nonLocked" id="${prefix}nonLocked" defaultChecked="true"/>  </TD>
</TR>
<TR>
	<TD align="right">备注:&nbsp;</TD><TD>&nbsp;<input type="textfield" name="memo" length="32" id="${prefix}memo"/></TD>
</TR>
</TABLE>
</td></tr>
<tr><td>授权选择:</td></tr>
<tr><td>
<div id="${prefix}auths" >
	
</div>
</td></tr>
<tr><td align="center"><input type="button" name="保存" value="submit" onclick="updateRole();"/></td></tr>
</table>
</form>
<script>
getAllAuthInEditRole();
</script> 
</body>
</html>

